<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/5/29
  Time: 8:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="/wechat/home/highSty.css">
    <jsp:include page="../public/head.jsp" flush="true" />
</head>
<body>
    <jsp:include page="../public/header.jsp" flush="true" />
    <div class="my_highsty">
        <!-- 导航 -->
        <section >
            <div class="out">
                <div class="time"><i class="iconfont icon-me">&#xe626;</i><span class="s1">打烊倒计时:</span><span class="s2">${productClass.closetime}</span></div>
                <%--<div class="sold"><span class="s1">销量</span><span class="s2">${scout}</span></div>--%>
            </div>
            <!--/out-->
            <div class="out2">请选择送餐时间</div>
            <!--/out2-->
            <form id="week" action="/home/shopCar.do" method="post" class="out3" >
                <c:forEach items="${week}" var="item" varStatus="status">
                    <%--<div>${item.weekName} ${item.date}<input type="radio" name="week" ${item.state} value="${item.rdate}" ></div>--%>
                    <div class="cont">
                        <input class="myDate" type="radio" name="week" id="radio${status.index}" ${item.state} value="${item.rdate}" onclick="check('i${status.index}')"  onchange="change(this)" >
                        <!--&#xe61c;-->
                        <span class="s1">${item.weekName}</span><span class="s2">${item.date} <i class="icon ${item.state}" id="i${status.index}"></i></span>
                    </div>
                </c:forEach>
                <input type="hidden" name="order" id="order">
                <input  name="totalCount" type="hidden" id="totalCount">
                <input  name="totalPrice" type="hidden" id="totalPrice">
            </form>

            <div class="cau_pos" >
                <c:forEach items="${products}" var="item" varStatus="status">
                    <form id="form${status.index}" >
                            <div class="wrap">
                                <div class="cont">
                                    <div class="pic">
                                        <a><img src="${item.image}" onclick="productDetail(${item.id},${item.productClassId})"></a>
                                        <div class="mask"></div>
                                        <div class="picname"><a>${item.name}</a></div>
                                    </div>
                                    <div class="info">

                                        <div class="txt1"><div class="price1">${item.des}<%--<span>(限10份)</span> --%></div></div>
                                        <div class="txt2"><div class="price1"><span class="s1">￥${item.price}</span>/份</div>
                                            <div class="num">
                                                <div class="n1 current" onclick="countSub('${status.index}')">-</div>
                                                <div class="n2" id="scount${status.index}">0</div>
                                                <div class="n3" onclick="countAdd('${status.index}')">+</div>
                                            </div></div>
                                    </div>
                                </div>
                                <!--/cont-->
                            </div>
                            <input type="hidden" value="${item.price}" name="goodPrcie" id="goodPrcie${status.index}" />
                            <input type="hidden" value="${item.id}" name="goodId" />
                            <input type="hidden" value="${item.image}" name="goodNormalImage" />
                            <input type="hidden" value="${item.name}" name="goodName" />
                            <input type="hidden" value="${item.productClassId}" name="productClassId" />
                            <input type="hidden" value="0" name="goodAccount" id="count${status.index}" />
                    </form>
                </c:forEach>
            </div>
            <!--/out4-->
            <div class="ps" id="pullRefresh">正在加载</div>
            <span class="s2" id="total" hidden>0.0</span>
            <span class="no" id="count" hidden>0</span>
            <div class="opra">
                <input type="button" name="" class="btn3" value="立即购买" onclick="buy()">
            </div>
        </section>
    </div>

<script>
    var isLast = false;
    var index = 0;
    var size = 100;







    function productDetail(id,productClassId) {
        var tmp = $("#week").serializeObject();
        var date = tmp.week;
        window.location.href = "/home/beforelogin/productDetail.do?id="+id+"&rdate="+date+"&type=2&begin_s=0&end_s=10&productClassId="+productClassId;
    }

    var canadd = true;

    $(function () {
        var now = new Date();
        var time = new Date(now.getFullYear()+'/'+(now.getMonth()+1)+"/"+now.getDate()+" ${productClass.closetime}");

        if (now > time){
            alert("商铺已打烊");
            canadd = false;
            $(".opra").hide();
        }
    });

    function countAdd(id) {
        if (!canadd){
            alert("商铺已打烊");
            return;
        }

        var current = $('#count'+id).val();
        var currentCount = $("#count").text();
        var currenttotal = $("#total").text();
        var price = $('#goodPrcie'+id).val();
        current++;
        currentCount++;
        currenttotal = (parseFloat(price)+parseFloat(currenttotal)).toFixed(1);
        $("#count"+id).val(current);
        $("#scount"+id).text(current) ;
        $("#count").text(currentCount);
        $("#total").text(currenttotal);

        if (current == 1){
            $("#add"+id).toggle();
            $("#addSub"+id).toggle();
        }
    }
    function countSub(id) {
        if (!canadd){
            alert("商铺已打烊");
            return;
        }

        var current = $('#count'+id).val();
        var currentCount = $("#count").text();
        var currenttotal = $("#total").text();
        if (current == 0)
                return;
        current--;
        currentCount--;
        currenttotal = (parseFloat(currenttotal) - parseFloat($('#goodPrcie'+id).val())).toFixed(1);
        $("#count"+id).val(current);
        $("#scount"+id).text(current) ;
        $("#count").text(currentCount);
        $("#total").text(currenttotal);

        if (current == 0){
            $("#add"+id).toggle();
            $("#addSub"+id).toggle();
        }
    }

    function buy(){
        var forms = $("form:not([id='week'])");
        var goods = [];
        var tmp = $("#week").serializeObject();
        var key = tmp.week;
        var size = forms.size();

        for(var i = 0;i<size;i++){
            var temp = $("#"+forms[i].id).serializeObject();
            if (temp.goodAccount > 0) {
                temp.shopId = key;
                goods.push(temp);
            }
        }
        if (goods.length > 0) {
            var data = '{"' + key + '":' + JSON.stringify(goods) + '}';
            $("#order").val(data);
            $("#totalCount").val($("#count").text());
            $("#totalPrice").val($("#total").text());
            $("#week").submit();
        }
    }

    function check(id) {
        $("i").removeClass("checked");
        $("#"+id).addClass("checked");
    }

    function change(item){

        //现在的时间
        var now = new Date();
        //打烊的时间
        var time = new Date(now.getFullYear()+'/'+(now.getMonth()+1)+"/"+now.getDate()+" ${productClass.closetime}");

        //选择送达的时间
        var selectday = $(item).val();
        var selectdate = new Date(selectday);
        var select =  selectdate.getDate();
        //不允许的情况:   现在的时间与送达的时间一样且 打烊了
        //允许的情况
        var nextdate = new Date((now.valueOf()+1*24*60*60*1000));

        if (now > time &&  nextdate.getDate() == select){
            alert("商铺已打烊");
            canadd = false;
            $(".buy").hide();
        }else{
            $(".buy").show();
            canadd = true;
        }

    }

    $.fn.serializeObject = function()
    {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };




</script>
</body>

</html>
